package com.example.demo2.student;

import com.example.demo2.Util.DBHelper;
import com.example.demo2.pojo.student;
import javax.servlet.annotation.WebServlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "FindStudentByname", value = "/FindStudentByname")
public class FindStudentByname extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<student> studentList = new ArrayList<>();
        DBHelper dbHelper = new DBHelper();
        conn = dbHelper.getConn();
        try {
            preparedStatement = conn.prepareStatement("select *from webdesign.student");
            resultSet = preparedStatement.executeQuery();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            while (resultSet.next()){
                student student = new student();
                student.setSid(resultSet.getInt("sid"));
                student.setName(resultSet.getString("name"));
                student.setGender(resultSet.getString("gender"));
                student.setAge(resultSet.getInt("age"));
                student.setClass1(resultSet.getString("class"));
                student.setMajor(resultSet.getString("major"));
                studentList.add(student);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            dbHelper.closeAll(conn,preparedStatement,resultSet);
        }
        req.setAttribute("list", studentList);
        RequestDispatcher dispatcher = req.getRequestDispatcher("studentlist.jsp");
        dispatcher.forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<student> studentList = new ArrayList<>();
        DBHelper dbHelper = new DBHelper();
        conn = dbHelper.getConn();
        try {
            preparedStatement = conn.prepareStatement("select *from webdesign.student");
            resultSet = preparedStatement.executeQuery();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            if(resultSet.next()){

                student student = new student();
                student.setSid(resultSet.getInt("sid"));
                student.setName(resultSet.getString("name"));
                student.setGender(resultSet.getString("gender"));
                student.setAge(resultSet.getInt("age"));
                student.setClass1(resultSet.getString("class"));
                student.setMajor(resultSet.getString("major"));
                studentList.add(student);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            dbHelper.closeAll(conn,preparedStatement,resultSet);
        }
        req.setAttribute("list", studentList);
        RequestDispatcher dispatcher = req.getRequestDispatcher("studentlist.jsp");
        dispatcher.forward(req, resp);
    }
}
